package com.qfedu.yanglao20admin.advertising.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.CommonResult;
import com.qfedu.yanglao20.common.utils.OssUtils;
import com.qfedu.yanglao20.entity.advertising.AdvertisingContent;
import com.qfedu.yanglao20admin.advertising.param.AdvertisingContenParam;
import com.qfedu.yanglao20admin.advertising.service.AdvertisingContenService;
import com.qfedu.yanglao20admin.advertising.vo.AdvertisingContentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Api(tags = "广告内容管理")
@RestController
@RequestMapping("/AdvertisingConten")
public class AdvertisingContenController {

    @Resource
    private AdvertisingContenService advertisingContenService;

    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @GetMapping("/delete")
    public CommonResult deleteAdvertisingContent(Integer id) {
        advertisingContenService.deleteById(id);
        return CommonResult.success();
    }

    @ApiOperation(value = "展示广告内容数据", notes = "展示广告内容数据")
    @GetMapping("/list")
    public CommonResult<AdvertisingContenParam> advertisingContentList(AdvertisingContenParam param) {
        Page<AdvertisingContentVO> advertisingContentVOPageVO = advertisingContenService.pageAdvertisingContent(param);
        return CommonResult.success(advertisingContentVOPageVO);
    }

    @ApiOperation(value = "添加广告内容", notes = "添加广告内容")
    @PostMapping("/add")
    public CommonResult addAdvertisingContent(AdvertisingContentVO advertisingContentVO) {
        advertisingContenService.add(advertisingContentVO);
        return CommonResult.success();
    }

    @ApiOperation(value = "根据id查询广告内容信息", notes = "根据id查询广告内容信息")
    @GetMapping("/query")
    public CommonResult queryAdvertisingContent(@RequestParam @ApiParam("广告内容信息id") Integer id) {
        AdvertisingContentVO advertisingContentVO = advertisingContenService.queryAdverisingContent(id);
        return CommonResult.success(advertisingContentVO);
    }

    @ApiOperation(value = "修改广告内容信息", notes = "修改广告内容信息")
    @PostMapping("/update")
    public CommonResult<AdvertisingContentVO> updateAdvertisingContent(AdvertisingContentVO advertisingContentVO) {
        advertisingContenService.update(advertisingContentVO);
        return CommonResult.success();
    }

    @ApiOperation(value = "上传广告图片", notes = "上传广告图片")
    @PostMapping("/upload")
    public CommonResult<AdvertisingContent> upload(MultipartFile file) {

        String path = null;
        try {
            path = OssUtils.uploadFile2OSS(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.success(path);
    }
}